
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Abahe
 */
public class GameBoard extends GameCanvas implements Runnable {

    MMIDlet midlet;
    //______________________________
    //Grid
    private Grid grid[][] = new Grid[10][4];
    //______________________________
    //jumlah
    private int JUM = 40;
    private int MAX_RANGE = 12;
    //______________________________
    //speed
    private int speedGame = 500;
    private long time = 0;
    //______________________________
    //testing speed
    private int iTestSpeed = 0;
    //______________________________
    //SPRITE
    private Sprite spSetAlertLeft;
    private Sprite spSetAlertRight;
    private MatterBlock matterBlock[] = new MatterBlock[JUM];
    private Sprite spAncurBlock[] = new Sprite[JUM];
    private LayerManager lmgr;
    //______________________________
    //image
    private Image imgSetBarBlue;
    private Image imgSetBarRed;
    private Image imgSetBarYellow;
    private Image imgAncurBlockBlue;
    private Image imgAncurBlockRed;
    private Image imgAncurBlockYellow;
    private Image imgSetBarBowl;
    private Image imgSetBgBowl;
    private Image imgSetBarScore;
    private Image imgSet;
    private Image imgPortal;
    private Image imgGrid;
    private String pathImgSet;
    //______________________________
    //xy
    private int xSetBarBowl;
    private int ySetBarBowl;
    private int xSetBgBowl;
    private int ySetBgBowl;
    private int xSetBarScore;
    private int ySetBarScore;
    private int xGrid;
    private int yGrid;
    //______________________________
    //size
    private int iSetBarScoreWidth = 240;
    private int iSetBarScoreHeight = 29;
    private int iSetBgBowlWidth = 240;
    private int iSetBgBowlHeight = 211;
    private int iSetBarWidth = 58;
    private int iSetBarHeight = 19;
    //______________________________
    //INT
    private int dasar = 192;
    //______________________________
    //random
    private Random myRandom = new Random();
    //______________________________
    //thread
    private boolean running = false;

    public GameBoard(MMIDlet midlet) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;

        if (MMIDlet.LANDSCAPE == 1) {
            pathImgSet = "/set_landscape_80x240.png";
        } else {
            pathImgSet = "/set_portrait_80x240.png";
        }

        try {
            spSetAlertLeft = new Sprite(Image.createImage("/set_alertleft_26x11.png"),
                    26, 10);
            spSetAlertRight = new Sprite(Image.createImage("/set_alertright_26x11.png"),
                    26, 10);
            imgSetBarBowl = Image.createImage("/set_barbowl_238x210.png");
            imgSetBgBowl = Image.createImage("/set_bgbowl_240x211.png");
            imgSetBarScore = Image.createImage("/set_barscore_240x29.png");
            imgSetBarBlue = Image.createImage("/set_barblue_58x19.png");
            imgSetBarRed = Image.createImage("/set_barred_58x19.png");
            imgSetBarYellow = Image.createImage("/set_baryellow_58x19.png");
            imgAncurBlockBlue = Image.createImage("/set_barblueclink_60x28.png");
            imgAncurBlockRed = Image.createImage("/set_barredclink_60x28.png");
            imgAncurBlockYellow = Image.createImage("/set_baryellowclink_60x28.png");
            imgSet = Image.createImage(pathImgSet);
            imgPortal = Image.createImage("/set_portallimit_190x6.png");
            imgGrid = Image.createImage("/grid.png");
        } catch (IOException ex) {
        }
        if (MMIDlet.LANDSCAPE == 1) {
            xSetBgBowl = MMIDlet.WIDTH - iSetBgBowlWidth;
            ySetBgBowl = MMIDlet.HEIGHT - (iSetBarScoreHeight + iSetBgBowlHeight);
            xSetBarScore = MMIDlet.WIDTH - iSetBarScoreWidth;
            ySetBarScore = MMIDlet.HEIGHT - iSetBarScoreHeight;
        } else {
            xSetBgBowl = 0;
            ySetBgBowl = MMIDlet.HEIGHT - (iSetBarScoreHeight + iSetBgBowlHeight);
            //ySetBgBowl = MMIDlet.HEIGHT - iSetBarScoreHeight;
            xSetBarScore = MMIDlet.WIDTH - iSetBarScoreWidth;
            ySetBarScore = MMIDlet.HEIGHT - iSetBarScoreHeight;
        }

        xSetBarBowl = xSetBgBowl + 1;
        ySetBarBowl = ySetBgBowl;

        xGrid = xSetBarBowl + 4;
        yGrid = MMIDlet.HEIGHT - (iSetBarHeight * 10 + iSetBarScoreHeight);

        for (int i = 0; i < JUM; i++) {
            int warna = getRandomInt(3);
            if (warna == 0) {
                matterBlock[i] = new MatterBlock(imgSetBarRed, 0, 0);
                spAncurBlock[i] = new Sprite(imgAncurBlockRed, 60, 28);
            } else if (warna == 1) {
                matterBlock[i] = new MatterBlock(imgSetBarYellow, 0, 0);
                spAncurBlock[i] = new Sprite(imgAncurBlockYellow, 60, 28);
            } else if (warna == 2) {
                matterBlock[i] = new MatterBlock(imgSetBarBlue, 0, 0);
                spAncurBlock[i] = new Sprite(imgAncurBlockBlue, 60, 28);
            }
        }

        lmgr = new LayerManager();
        lmgr.append(spSetAlertLeft);
        lmgr.append(spSetAlertRight);
        for (int i = 0; i < JUM; i++) {
            lmgr.append(matterBlock[i]);
            lmgr.append(spAncurBlock[i]);
        }
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 4; j++) {
                grid[i][j] = new Grid(imgGrid);
                grid[i][j].setPosition(xGrid + (j * iSetBarWidth), yGrid + (i * iSetBarHeight));
                lmgr.append(grid[i][j]);
            }
        }

        matterBlock[0].setPosition(xGrid, yGrid);

        spSetAlertLeft.setPosition(xSetBgBowl, ySetBgBowl);
        spSetAlertRight.setPosition(xSetBgBowl + spSetAlertLeft.getWidth() + imgPortal.getWidth() - 2,
                ySetBgBowl);


    }

    //start thread
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    public final void run() {
        Graphics g = getGraphics();
        while (running) {
            time = System.currentTimeMillis();
            checkForCollision();
            //System.out.println("kesini ga?");
            iTestSpeed++;
            drawDisplay(g);
            time = speedGame - (System.currentTimeMillis() - time);
            try {
                //Thread.sleep(speedGame);
                Thread.sleep((time < 0 ? 0 : time));
            } catch (InterruptedException ie) {
            }
        }
    }

    private void drawDisplay(Graphics g) {
        g.setColor(0xffccff);
        g.fillRect(0, 0, MMIDlet.WIDTH, MMIDlet.HEIGHT);

        g.drawImage(imgSet, 0, 0, Graphics.TOP | Graphics.LEFT);
        g.drawImage(imgSetBarBowl, xSetBarBowl, ySetBarBowl, Graphics.TOP | Graphics.LEFT);
        g.drawImage(imgPortal, xSetBgBowl + spSetAlertLeft.getWidth() - 1, ySetBgBowl + 2, Graphics.TOP | Graphics.LEFT);
        g.drawImage(imgSetBarScore, xSetBarScore, ySetBarScore, Graphics.TOP | Graphics.LEFT);

        downBlock();
        lmgr.paint(g, 0, 0);
        spSetAlertLeft.nextFrame();
        spSetAlertRight.nextFrame();

        drawGrid(g);

        flushGraphics();
    }

    private void checkForCollision() {
    }

    private void downBlock() {
        for (int k = 0; k < JUM; k++) {
            if (matterBlock[0].getY() != dasar) {
                matterBlock[0].move(0, 1);
            } else {
                //grid[i][j].setStatus(1);
                matterBlock[0].setStacked(true);
            }
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 4; j++) {
                    if (matterBlock[0].getY() == grid[i][j].getY()
                            && matterBlock[0].getX() == grid[i][j].getX()) {
                        System.out.println(grid[i][j].getY());
                        if(matterBlock[0].isStacked()){
                            grid[i][j].setStatus(1);
                        }
                    }
                }
            }
        }


    }

    private void drawGrid(Graphics g) {
        g.setColor(0x0000ff);
        for (int i = 0; i < 10; i++) {
            g.drawLine(xGrid, yGrid + (i * iSetBarHeight),
                    xGrid + iSetBgBowlWidth, yGrid + (i * iSetBarHeight));

        }

        g.setColor(0x000000);
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 4; j++) {
                //g.drawImage(imgSetBarRed,
                //        xGrid + (j * iSetBarWidth), yGrid + (i * iSetBarHeight),
                //    Graphics.TOP | Graphics.LEFT);

                g.drawString(String.valueOf(grid[i][j].getStatus()),
                        xGrid + (j * iSetBarWidth), yGrid + (i * iSetBarHeight),
                        Graphics.TOP | Graphics.LEFT);


            }
        }
    }

    protected void keyPressed(int keyCode) {
        if (keyCode == -7) {
            midlet.exit();
        }
    }

    public void stop() {
        running = false;
    }

    private int getRandomInt(int upper) {
        int retVal = myRandom.nextInt() % upper;
        if (retVal < 0) {
            retVal += upper;
        }
        return (retVal);
    }
}
